package web.user;

import bean.Type;
import bean.User;
import com.alibaba.fastjson.JSON;
import entity.UserEntity;
import util.FANREN;

import javax.persistence.EntityManager;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "UserEdit",urlPatterns = "/dao/update/userEdit")
public class UserEdit extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String jsonString = FANREN.getJson(request);
        User user = JSON.parseObject(jsonString,User.class);
        EntityManager em = (EntityManager) request.getAttribute("_entity.Manager");
        UserEntity userEntity = null;
        if(user.getUid().equals("")) {
            //新增
            userEntity = new UserEntity();
        } else {
            //修改
            userEntity = em.find(UserEntity.class,Integer.parseInt(user.getUid()));
        }
        userEntity.setVip(user.getVip());
        userEntity.setPhone(user.getPhone());
        userEntity.setEmail(user.getEmail());
        userEntity.setName(user.getName());
        em.persist(userEntity);
    }
}
